import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/entity/user/VerificationMode.dart';
import 'package:mahjong/page/common/RoomMessage.dart';
import 'package:mahjong/page/common/XpockerTips.dart';
import 'package:mahjong/widget/dialog/VerificationWidget.dart';
import 'package:mahjong/util/ConstState.dart';
import 'package:flutter/animation.dart';

typedef _callBack = void Function(VerificationMode data);

class CustomerDialog {
  static showVerification(context, _callBack callBack) {
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return VerificationWidget(callBack: callBack);
        });
  }

  static showRoomMessage(String tips, {String userName, String headImage}) {
    BotToast.showText(
      text: tips,
      wrapAnimation: (controller, cancelFunc, widget) {
        return RoomMessage(
          controller: controller,
          userName: userName,
          headImage: headImage,
          tips: tips,
        );
      },
    );
  }

  static showText(String tips,
      {Color textColor, Color bottomColor: Colors.white}) {
    if (textColor == null) {
      textColor = Color(0xff703007);
    }
    BotToast.showText(
      text: tips,
      wrapAnimation: (controller, cancelFunc, widget) {
        return XpockerTips(
            controller: controller,
            tips: tips,
            textColor: textColor,
            bottomColor: bottomColor);
      },
    );
  }

  static loading(String msg) {
    if (!ConstState.getInstance().loadingDialogShow) {
      ConstState.getInstance().loadingDialogShow = true;
      BotToast.showAnimationWidget(
          groupKey: BotToast.loadKey,
          enableKeyboardSafeArea: true,
          backButtonBehavior: BackButtonBehavior.ignore,
          clickClose: false,
          allowClick: false,
          ignoreContentClick: false,
          crossPage: false,
          toastBuilder: (context) {
            return Center(
              child: Container(
                width: ScreenUtil().screenHeight / 2,
                height: ScreenUtil().screenHeight / 2,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    CircularProgressIndicator(),
                    Text(
                      msg,
                      style: TextStyle(color: Colors.white, fontSize: 20.sp),
                    )
                  ],
                ),
              ),
            );
          },
          animationDuration: const Duration(milliseconds: 300),
          wrapToastAnimation: (controller, cancel, child) {
            return SafeArea(child: child);
          },
          onlyOne: true,
          backgroundColor: Colors.black26);
    }
  }

  static void closeAllLoading() {
    ConstState.getInstance().loadingDialogShow = false;
    BotToast.closeAllLoading();
  }
}

class LoadingDialog extends Dialog {
  final String text;

  LoadingDialog({Key key, @required this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 弹窗的时候禁止返回
    return new WillPopScope(
        onWillPop: () async => false,
        child: new Material(
          //创建透明层
          type: MaterialType.transparency, //透明类型
          child: new Center(
            //保证控件居中效果
            child: new SizedBox(
              width: 120.0,
              height: 120.0,
              child: new Container(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    new CircularProgressIndicator(
                      valueColor:
                          new AlwaysStoppedAnimation<Color>(Colors.white),
                    ),
                    new Padding(
                      padding: const EdgeInsets.only(
                        top: 20.0,
                      ),
                      child: new Text(
                        text,
                        style:
                            new TextStyle(fontSize: 12.0, color: Colors.white),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ));
  }
}
